package com.bree.erp.jsf.view;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.bree.erp.entity.Project;
import com.bree.erp.entity.Warehouse;
import com.bree.erp.jsf.datatable.LazyBreeDataModel;
import com.bree.erp.service.ProjectService;
import com.bree.erp.service.WarehouseService;

@ManagedBean(name = "dtProjectLazyView")
@ViewScoped
public class ProjectLazyView {

	private LazyDataModel<Project> lazyModel;

	@EJB
	private ProjectService projectService;
	
	@EJB
	private WarehouseService whService;

	private Project selectedItem;

	private Long projectId;
	private String code;
	private String description;
	private String jobNumber;
	private String warehouse;

	@PostConstruct
	public void init() {
		lazyModel = new LazyBreeDataModel<Project, ProjectService, Long>(
				projectService, 0L);
	}

	public void onRowSelect(SelectEvent event) {
		FacesMessage msg = new FacesMessage("Item Selected",
				((Project) event.getObject()).getId() + "");
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public void createProject() {
		
		projectService.create(code, description, jobNumber, warehouse);
		
		code = description = jobNumber = warehouse = null;
	}

	public void updateProject() {
		projectService.update(projectId, code, description, jobNumber, warehouse);
	}


	/**
	 * @return the lazyModel
	 */
	public LazyDataModel<Project> getLazyModel() {
		return lazyModel;
	}

	/**
	 * @return the selectedItem
	 */
	public Project getSelectedItem() {
		return selectedItem;
	}

	/**
	 * @param selectedItem
	 *            the selectedItem to set
	 */
	public void setSelectedItem(Project selectedItem) {
		this.selectedItem = selectedItem;

		if (selectedItem != null) {
			projectId = selectedItem.getId();
			code = selectedItem.getCode();
			description = selectedItem.getDescription();
			jobNumber = selectedItem.getJobNumber();
			warehouse = selectedItem.getDefaultWarehouse() == null ? null : selectedItem.getDefaultWarehouse().getId();
		} else {
			code = description = jobNumber = warehouse = null;
		}
	}
	
	public List<Warehouse> getWarehouses() {
		return whService.list();
	}

	/**
	 * @return the projectId
	 */
	public Long getProjectId() {
		return projectId;
	}

	/**
	 * @param projectId the projectId to set
	 */
	public void setProjectId(Long projectId) {
		this.projectId = projectId;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return the jobNumber
	 */
	public String getJobNumber() {
		return jobNumber;
	}

	/**
	 * @param jobNumber the jobNumber to set
	 */
	public void setJobNumber(String jobNumber) {
		this.jobNumber = jobNumber;
	}

	/**
	 * @return the warehouse
	 */
	public String getWarehouse() {
		return warehouse;
	}

	/**
	 * @param warehouse the warehouse to set
	 */
	public void setWarehouse(String warehouse) {
		this.warehouse = warehouse;
	}

}
